/*
 * @Author: 葛文杰
 * @Date: 2021/4/2 13:52
 * @Description: egg service 数据源接口参数配置
 */

const Service = require('egg').Service;
const DataSourceInterfaceParamProxy = require('../proxy/dataSourceInterfaceParamProxy');
const { buildTableData } = require('../util/utils');

class DataSourceInterfaceParam extends Service {

    /**
     * 获取数据源接口参数列表
     */
    async getInterfaceParamByDataSourceConfigId(interfaceParamCondition) {
        const conn = await this.app.mysql.beginTransaction();
        try {
            let { pageNo, pageSize, ...otherCondition } = interfaceParamCondition;
            otherCondition.pageNo = parseInt(pageNo);
            otherCondition.pageSize = parseInt(pageSize);
            const dataSourceInterfaceParamList =
              await DataSourceInterfaceParamProxy.getInterfaceParamListByDataSourceConfigId(conn, otherCondition);
            const total =
              await DataSourceInterfaceParamProxy.getInterfaceParamTotalByDataSourceConfigId(conn, otherCondition);
            await conn.commit(); // 提交事务
            return buildTableData(dataSourceInterfaceParamList, total, pageNo, pageSize);
        } catch (err) {
            await conn.rollback(); // 一定记得捕获异常后回滚事务！！
            throw err;
        }
    }

    /**
     * 获取数据源接口参数信息
     * @return {Promise<void>}
     */
    async getInterfaceParamInfoById(interfaceParamCondition) {
        const conn = await this.app.mysql.beginTransaction();
        try {
            let { dataSourceInterfaceParamId } = interfaceParamCondition;
            let condition = {
                id: dataSourceInterfaceParamId,
            };
            const dataSourceFieldInfo =
              await DataSourceInterfaceParamProxy.getInterfaceParamInfoById(conn, condition);
            await conn.commit(); // 提交事务
            return dataSourceFieldInfo;
        } catch (err) {
            await conn.rollback(); // 一定记得捕获异常后回滚事务！！
            throw err;
        }
    }

    /**
     * 新增数据源接口参数
     * @param interfaceParamCondition
     * @return {Promise<void>}
     */
    async insertInterfaceParamInfoById(interfaceParamCondition) {
        const conn = await this.app.mysql.beginTransaction(); // 初始化事务
        try {
            const result = await DataSourceInterfaceParamProxy.insertInterfaceParamInfoById(conn, interfaceParamCondition);
            await conn.commit();
            return result;
        } catch (err) {
            await conn.rollback();
            throw err;
        }
    }

    /**
     * 修改数据源接口参数
     * @param interfaceParamCondition
     * @return {Promise<void>}
     */
    async updateInterfaceParamInfoById(interfaceParamCondition) {
        const conn = await this.app.mysql.beginTransaction(); // 初始化事务
        try {
            let { dataSourceInterfaceParamId, ...condition } = interfaceParamCondition;
            condition.id = dataSourceInterfaceParamId;
            const result = await DataSourceInterfaceParamProxy.updateInterfaceParamInfoById(conn, condition);
            await conn.commit();
            return result;
        } catch (err) {
            await conn.rollback();
            throw err;
        }
    }

    /**
     * 删除数据源接口参数
     */
    async deleteInterfaceParamById(interfaceParamCondition) {
        const conn = await this.app.mysql.beginTransaction(); // 初始化事务
        try {
            let { dataSourceInterfaceParamId } = interfaceParamCondition;
            let condition = {
                id: dataSourceInterfaceParamId,
            };
            const result = await DataSourceInterfaceParamProxy.deleteInterfaceParamById(conn, condition);
            await conn.commit();
            return result;
        } catch (err) {
            await conn.rollback();
            throw err;
        }
    }
}

module.exports = DataSourceInterfaceParam;
